package com.lanou3g.ptp.organize.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.ptp.entity.College;
import com.lanou3g.ptp.entity.Major;
import com.lanou3g.ptp.entity.School;
import com.lanou3g.ptp.entity.User;
import com.lanou3g.ptp.organize.mapper.CollegeMapper;
import com.lanou3g.ptp.organize.service.ClazzService;
import com.lanou3g.ptp.organize.service.CollegeService;
import com.lanou3g.ptp.organize.service.MajorService;
import com.lanou3g.ptp.organize.service.SchoolService;
import com.lanou3g.ptp.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Date;
import java.util.List;

@Service(timeout = 10000)
public class CollegeServiceImpl implements CollegeService {
    @Autowired
    private CollegeMapper collegeMapper;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private SchoolService schoolService;
    @Reference
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${REDIS_COLLEGES_KEY_PREFIX}")
    private String REDIS_COLLEGES_KEY_PREFIX;

    @Override
    public void addCollege(College college) {
        Date now = new Date();
        college.setCreateTime(now);
        college.setUpdateTime(now);
        college.setStatus(0);
        collegeMapper.addCollege(college);
        cacheColleges(college.getSchoolId());
    }

    @Override
    public College findById(Long id) {
        return collegeMapper.findById(id);
    }

    @Override
    public List<College> findByIds(Long[] ids) {
        List<College> colleges = collegeMapper.findByIds(ids);
        return colleges;
    }

    @Override
    public College findByName(String name) {
        College college = collegeMapper.findByName(name);
        return college;
    }

    @Override
    public List<College> findAll(Integer page, Integer size) {
        Page<College> pages = PageHelper.startPage(page, size);
        List<College> colleges = collegeMapper.findAll();
        return pages;
    }

    @Override
    public void updateById(College college) {
        college.setUpdateTime(new Date());
        collegeMapper.updateById(college);
        cacheColleges(college.getSchoolId());
    }

    @Override
    public void deleteById(Long id) {
        College college = findById(id);
        collegeMapper.deleteById(id);
        cacheColleges(college.getSchoolId());
    }

    @Override
    public void deleteByIds(Long[] ids) {
        collegeMapper.deleteByIds(ids);
    }

    @Override
    public List<College> findOrgNodesBySchoolId(Long schoolId) {
        String key = REDIS_COLLEGES_KEY_PREFIX + schoolId;
        String json = redisTemplate.opsForValue().get(key);
        if (json != null) {
            return JSON.parseArray(json, College.class);
        } else {
            return findOrgsFromDB(schoolId);
        }
    }

    private List<College> findOrgsFromDB(Long schoolId) {
        List<College> colleges = findBySchoolId(schoolId);
        School school = schoolService.findById(schoolId);
        for (College college : colleges) {
            Long collegeLeaderId = college.getLeaderId();
            if (collegeLeaderId != null) {
                User leader = userService.userById(collegeLeaderId);
                college.setLeaderName(leader.getName());
            }
            List<Major> majors = majorService.findByCollegeId(college.getId());
            college.setChildren(majors);
            college.setParentNode(school.getName());
            college.setParentId(schoolId);
            college.setLevel(1);
        }
        return colleges;
    }

    @Override
    public void cacheColleges(Long schoolId) {
        String key = REDIS_COLLEGES_KEY_PREFIX + schoolId;
        List<College> colleges = findOrgsFromDB(schoolId);
        redisTemplate.opsForValue().set(key, JSON.toJSONString(colleges));
    }

    @Override
    public List<College> findBySchoolId(Long schoolId) {
        return collegeMapper.findBySchoolId(schoolId);
    }

}
